package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import model.UserPermissions;
import client.CliMessage;
import client.IObserve;
import client.SGI;

public class LogoutListener extends BackListener implements IObserve 
{
	
	public LogoutListener(Observer previousObserver, JFrame windowToDispose)
	{
		super(previousObserver, windowToDispose);
	}

	private void sendToServer(String updateQuery)
	{
		try
		{
			SGI.clnt.sendToServer(updateQuery);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}	
	}
	
	private void logout()
	{
		super.changepreviousObserver(this);
		super.activatepreviousObserver();
		
		int uid = UserPermissions.getInstance(0, null, null).getCurrUserID();
		String updateQuery = "adu"	+"UPDATE user "
									+"SET loginStatus = 0 "
									+"WHERE uid = " +uid;
				
		sendToServer(updateQuery);
		
	}
	
	@Override
	public void windowClosing(WindowEvent e) 
	{	
		//initiate logout sequence
		logout();	
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		//initiate logout sequence
		logout();
	}
	

	@Override
	public void update(Observable o, Object arg) 
	{
		String msg = new String(((CliMessage)arg).getMessageFromServer());
		
		if(msg.equals("#OC:Connection established."))
		{
			//Do nothing.
			//maybe needs handling if it is after a reconnection try.
		}
		if( msg.equals("#OC:Connection error.") || msg.equals("#OC:Connection closed." ))
		{
			handleConnectionFail();
			return;
		}
		if( msg.equals("#Server: Action success"))
		{
			handleDBRequestSuccess();
			return;
		}
		if(msg.equals("#Server: Action failed" ))
		{
			handleDBRequestFail() ;
			return;
		}
		if(msg.equals("#Server: Null search result"))
		{	
			handleNullSearchResult();
			return;
		}
		if(msg.equals("#Server: Search result"))
		{ 
			//handleDBSearchResult((CliMessage)arg);
		}
		
	}

	@Override
	public void handleDBRequestFail() 
	{
		//Logout action failed - contact the system administrator
		JOptionPane.showMessageDialog(super.getWindowToDispose(), "Error: Server comunication problem.\n "
																	+"Try again later");
	}

	@Override
	public void handleDBRequestSuccess() {
		//dispose window and exit the program
		super.disposeWindow();
		System.exit(1);
		
	}

	@Override
	public void handleConnectionFail() {
		//Logout action failed - contact the system administrator
		JOptionPane.showMessageDialog(super.getWindowToDispose(), "Error: Server comunication problem.\n "
																+ "Logout action failed - contact the system administrator");
	}

	@Override
	public void handleNullSearchResult() {
		// No action needed
		
	}

	@Override
	public void handleDBSearchResult(CliMessage dbResult) {
		// No action needed
		
	}

	@Override
	public void handlePhoto(CliMessage msgWithTryImage) {
		// TODO Auto-generated method stub
		
	}
	

}
